package edu.gduf.controller;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.gduf.dto.QRCodeDTO;
import edu.gduf.service.QRCodeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author hhx
 *
 */

@Controller
@RequestMapping("/qrcode")
public class QRCodeController {
	@Autowired
	private QRCodeService qrCodeService;

	/**
	 * 
	 * @param tid
	 *            老师工号
	 * 
	 * @param minute
	 *            多少分钟内签订
	 * 
	 * @param response
	 * 
	 *            note: startTime 为请求
	 *            /qrcode/getQRCode/{starttime}/{minute}/{tid}/{courceId} 的时间
	 *            endTime = startTime + minute
	 */
	@ApiOperation(value = "二维码生成 ", notes = "前端用 img 标签即可 例如 : /qrcode/getQRCode/{time}/{classId}/{tid}/{courceName} (时间的pattern: yyyy-MM-dd HH:mm)")
	@ApiImplicitParams({ @ApiImplicitParam(name = "starttime", value = "签到时间开始", required = true, dataType = "string"),
			@ApiImplicitParam(name = "classId", value = "班级编号", required = true, dataType = "int"),
			@ApiImplicitParam(name = "tid", value = "老师工号", required = true, dataType = "int"),
			@ApiImplicitParam(name = "courceName", value = "课程名称", required = true, dataType = "string") })

	@RequestMapping(value = "/getQRCode/{time}/{classId}/{tid}/{courceName}", method = RequestMethod.GET)
	public void getQRCode(@PathVariable("time") Integer time, @PathVariable("tid") Integer tid,
			@PathVariable("classId") Integer classId, @PathVariable("courceName") String courceName,
			HttpServletResponse response, HttpServletRequest request, HttpSession session) {
		try {

			QRCodeDTO qrCodeDTO = new QRCodeDTO();
			qrCodeDTO.setClassId(classId);
			qrCodeDTO.settId(tid);

			DateTime parse = DateTime.now();

			qrCodeDTO.setStartTime(parse.getMillis());

			qrCodeDTO.setEndTime(parse.plusMinutes(time).getMillis());
			System.out.println(qrCodeDTO);

			session.setAttribute("siginInfo", qrCodeDTO);
			Object attribute = session.getAttribute("siginInfo");
			System.out.println("getQRCode-->" + attribute);

			// response.addHeader("pragma", "NO-cache");
			// response.addHeader("Cache-Control", "no-cache");
			// response.addDateHeader("Expries", 0);
			// response.setContentType("image/png");
			OutputStream out = response.getOutputStream();
			qrCodeService.createQRCode(tid, qrCodeDTO.getStartTime(), qrCodeDTO.getEndTime(), classId, courceName, out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	@RequestMapping(value = "/getCode", method = RequestMethod.GET)
	public void getCode(HttpServletResponse response, HttpServletRequest request,HttpSession session) throws Exception {
		response.addHeader("pragma", "NO-cache");
		response.addHeader("Cache-Control", "no-cache");
		response.addDateHeader("Expries", 0);
		response.setContentType("image/png");
		QRCodeDTO qrCodeDTO = (QRCodeDTO) session.getAttribute("siginInfo");


		String content = "http://hongrm.cn/signin/signinjsp?starttime=" + qrCodeDTO.getStartTime() + "&tid="
				+ qrCodeDTO.gettId() + "&endtime=" + qrCodeDTO.getEndTime() + "&classId=" + qrCodeDTO.getClassId();
		System.out.println(content);
		qrCodeService.getQRCode(response.getOutputStream(), content);

	}

}
